﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Gollie.Payment.Targetpay
{
	public class IDealCheck : IDeal
	{
		string transactionId;
		bool payed = false;
		string returnCode = "";

		public IDealCheck(string transaction_Id)
			: this(false, transaction_Id)
		{
		}

		public IDealCheck(bool testMode, string transaction_Id)
			: base(testMode)
		{
			Dictionary<string, string> args = new Dictionary<string, string>();
			args["rtlo"] = partnerId;
			args["trxid"] = transaction_Id;
			args["once"] = "1";

			//args["partnerid"] = this.partnerId;
			//args["transaction_id"] = transaction_Id;

			this.transactionId = transaction_Id;

			logger.Debug("Handling iDEAL transaction ID: {0}",  transaction_Id);

			this.ProcessRequest(this.checkUrl, args);
		}

		protected override void ProcessTargetPayResponse(string response)
		{
			if (response == null)
				throw new Exception("response is null");

			logger.Debug("Response: {0}", response);
			string returnCode = response.Substring(0, 6);
			this.returnCode = returnCode;

			if (returnCode == "000000")
			{
				this.payed = true;

				return;
			}

			this.LogReturnCode(returnCode);
		}

		public string ReturnCode
		{
			get { return this.returnCode; }
		}

		public string TransactionId
		{
			get { return this.transactionId; }
		}

		public bool Payed
		{
			get { return this.payed; }
		}
	}
}